﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.ActionFilters.Action;
 
using Kaiser.ControllerFactories;
using Kaiser.FilterProviders;
using Kaiser.Infrastructure;
using Kaiser.ModelBinders;
using Kaiser.Models;
using Kaiser.ViewEngines;

namespace Kaiser.Modules.Core
{
    public class CoreModule : Module
    {
        #region XoohooModule Members

        public override string ModuleName
        {
            get { return "Core"; }
        }

        public override void Initialize()
        {
            //设置DI映射
            SetupContainer();
            //设置控制器工厂
            SetupControllerFactory();
            //注册视图引擎
            RegisterViewEngines();
            //注册FilterProviders
            RegisterFilterProviders();
            //注册皮肤解析器
            RegisterSkinResolvers();
        }

        public override void RegisterFilters(FilterRegistryFilterProvider filterRegistry, GlobalFilterCollection globalFilters)
        {
           
         
            // UserActionFilter 将UserViewModel和MembershipUrlViewModel加入至 XoohooViewModule
            filterRegistry.Add(null, typeof(UserActionFilter));
            // ReturnUrl 加入至 XoohooViewModule
            //filterRegistry.Add(null, typeof(ReturnUrlActionFilter));
            // 防止CSRF攻击
           // filterRegistry.Add(null, typeof(AntiForgeryAuthorizationFilter));
            // ViewEnginesResultFilter,ViewEngines设置
            // Skinning 从这里开始
            //filterRegistry.Add(null, typeof(ViewEnginesResultFilter));
 
        }

        public override void RegisterModelBinders(ModelBinderDictionary modelBinders)
        {
            modelBinders[typeof(PagingInfo)] = DependencyInjector.GetService<PagingInfoModelBinder>();
            //modelBinders[typeof(List<Guid>)] = DependencyInjector.GetService<GuidListModelBinder>();
            //modelBinders[typeof(List<Int32>)] = DependencyInjector.GetService<Int32ListModelBinder>();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 设置DI映射
        /// </summary>
        private void SetupContainer()
        {

            //DependencyInjector
            //    .RegisterType<IActionInvoker, KaiserControllerActionInvoker>();
            
 
            //DependencyInjector
            //    .RegisterType<IFormsAuthentication, FormsAuthenticationWrapper>();

        }

        /// <summary>
        /// 设置控制器工厂
        /// <remarks>主要目的是在ControllerFactory的GetControllerInstance方法中获取Controller实例后，重新设置其ActionInvoker</remarks>
        /// </summary>
        private void SetupControllerFactory()
        {
            //方式1：
            //实现一个IControllerFactory并RegisterType注册，不能再使用SetControllerFactory设置
            //DependencyInjector.RegisterType<IControllerFactory, DependencyInjectorControllerFactory>();
            //方式2：
            //实现一个IControllerFactory创建对象，再使用SetControllerFactory设置
            ControllerBuilder.Current.SetControllerFactory(DependencyInjector.GetService<DependencyInjectorControllerFactory>());
            //方式3：
            //实现一个IControllerActivator，如：NewControllerActivator,并RegisterType注册
            //DependencyInjector.RegisterType<IControllerActivator, NewControllerActivator>();
            //方式4：
            //实现一个IControllerActivator，如：NewControllerActivator,并创建对象
            //再使用DefaultControllerFactory(IControllerActivator controllerActivator)构造
            //最后使用SetControllerFactory设置
            //IControllerActivator activator = new NewControllerActivator();
            //DefaultControllerFactory controllerFactory = new DefaultControllerFactory(activator);
            //ControllerBuilder.Current.SetControllerFactory(controllerFactory);

        }

        /// <summary>
        /// 注册视图引擎
        /// </summary>
        private void RegisterViewEngines()
        {
            //获取ASP.NET MVC内置的视图引擎
            System.Web.Mvc.ViewEngines.Engines.Clear();

            System.Web.Mvc.ViewEngines.Engines.Add(DependencyInjector.GetService<KaiserRazorViewEngine>());

            //AreaRegistration.RegisterAllAreas();
 
        }

        /// <summary>
        /// 注册FilterProviders
        /// </summary>
        private void RegisterFilterProviders()
        {
            FilterProviderCollection filterProviders = DependencyInjector.GetService<FilterProviderCollection>();
            filterProviders.Add(DependencyInjector.GetService<FilterRegistryFilterProvider>());
        }

        /// <summary>
        /// 注册皮肤解析器
        /// </summary>
        private void RegisterSkinResolvers()
        {
            //ISkinResolverRegistry skinResolverRegistry = DependencyInjector.GetService<SkinResolverRegistry>();
            //skinResolverRegistry.Default = DependencyInjector.GetService<XoohooSkinResolver>();

            ////移动设备
            //skinResolverRegistry.Add(DependencyInjector.GetService<MobileSkinResolver>());
            ////IE6、IE7和IE8等Hacks
            //skinResolverRegistry.Add(DependencyInjector.GetService<LegacySkinResolver>());

            //DependencyInjector.RegisterInstance(skinResolverRegistry);
        }

        #endregion
    }
}
